package com.dk.controller;

import com.dk.data.Result;
import com.dk.domain.TCarDTO;
import com.dk.em.ConstantEnum;
import com.dk.pojo.TCar;
import com.dk.service.ITCarService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/tCarController")
public class TCarController {

    @Autowired
    private ITCarService itCarService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @ApiOperation(value = "车辆审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "车辆的主键",required = true,paramType = "path"),
            @ApiImplicitParam(name = "status",value = "车辆的状态",required = true,paramType = "path"),
            @ApiImplicitParam(name = "sellerId",value = "销售人员id",required = true,paramType = "path")

    })
    @GetMapping("/{id}/{status}/{sellerId}")
    public Result updateStatus(@PathVariable("id") Long id,@PathVariable("status")Integer status,@PathVariable("sellerId")String sellerId){
        TCar tCar = itCarService.getById(id);
        //修改车辆的状态,审核通过后通过mq把车辆的id发送出去
        int i = itCarService.updateStatus(tCar,status,sellerId);
        rocketMQTemplate.convertAndSend("id",id);  //生产者发送消息
        return Result.bulid(ConstantEnum.SUCCESS).setResult(i);
    }

    @ApiOperation(value = "车辆增加操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tCarDTO",value = "实体类:车辆",required = true,paramType = "body",dataTypeClass = TCar.class)
    })
    @PostMapping("/add")
    public Result add(@RequestBody TCarDTO tCarDTO){
        TCar tCar = new TCar();
        tCarDTO.setDeleted(0);
        tCarDTO.setVersion(0);
        tCarDTO.setCreateTime(new Date());
        tCarDTO.setUpdateTime(new Date());
        BeanUtils.copyProperties(tCarDTO,tCar);
        boolean b = itCarService.save(tCar);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("增加成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("增加失败");
    }

    @ApiOperation(value = "车辆信息回显")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "车辆主键",required = true,paramType = "path")
    })
    @GetMapping(value = "/selectById/{id}")
    public Result selectById(@PathVariable("id") Long id){
        TCar tCar = itCarService.getById(id);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(tCar);
    }

    @ApiOperation(value = "车辆信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tCar",value = "车辆实体类",required = true,paramType = "body")
    })
    @PutMapping(value = "/update")
    public Result updateById(@RequestBody TCarDTO tCarDTO){
        TCar tCar = new TCar();
        TCarDTO tCarDTO1 = TCarDTO.builder().id(tCarDTO.getId()).build();
        BeanUtils.copyProperties(tCarDTO1,tCar);
        boolean b = itCarService.updateById(tCar);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("修改成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("修改失败");
    }

    @ApiOperation(value = "车辆信息逻辑删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "车辆主键id",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result deleteById(@RequestParam Long id){
        boolean b = itCarService.removeById(id);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("删除成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("删除失败");
    }
}
